package com.l024.meet.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.l024.framework.helper.GlideHelper;
import com.l024.framework.model.AddFriendModel;
import com.l024.framework.utils.CommonUtils;
import com.l024.meet.R;

import java.util.List;

public class AddFriendAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    //标题
    public static final int TYPE_TITLE = 0;
    public static final int TYPE_CONTENT = 1;

    private List<AddFriendModel> friends;
    private Context mContext;
    private LayoutInflater inflater;
    private OnClickListener onClickListener;

    public AddFriendAdapter(Context context, List<AddFriendModel> friends) {
        this.mContext = context;
        this.friends = friends;
        inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void setOnClickListener(OnClickListener listener){
        this.onClickListener = listener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if(viewType==TYPE_TITLE){
            return new TitleViewHolder(inflater.inflate(R.layout.layout_search_title_item,null ));
        }else if(viewType==TYPE_CONTENT){
            return new CommonViewHolder(inflater.inflate(R.layout.layout_search_user_item,null ));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        AddFriendModel addFriendModel = friends.get(position);
        if(addFriendModel.getType()==TYPE_TITLE){
            //设置标题
            ((TitleViewHolder)holder).tvTitle.setText(addFriendModel.getTitle());
        }else if(addFriendModel.getType()==TYPE_CONTENT){
            //设置头像
            GlideHelper.loadUrl(mContext,addFriendModel.getPhoto(),((CommonViewHolder)holder).ivPhoto);
            //设置性别
            ((CommonViewHolder)holder).ivSex.setImageResource(addFriendModel.isSex()?R.drawable.img_boy_icon:R.drawable.img_girl_icon);
            ((CommonViewHolder)holder).tvNickname.setText(addFriendModel.getNickName());
            ((CommonViewHolder)holder).tvAge.setText(addFriendModel.getAge()+"岁");
            //描述
            ((CommonViewHolder)holder).tvDesc.setText(addFriendModel.getDesc());
            if(addFriendModel.isContact()){
                ((CommonViewHolder)holder).llContactInfo.setVisibility(View.VISIBLE);
                ((CommonViewHolder)holder).tvContactName.setText(addFriendModel.getContactName());
                ((CommonViewHolder)holder).tvContactPhone.setText(addFriendModel.getContactPhone());
            }
        }
        //点击事件
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onClickListener!=null){
                    onClickListener.OnClick(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return CommonUtils.isEmpty(friends)?friends.size():0;
    }

    @Override
    public int getItemViewType(int position) {
        return friends.get(position).getType();
    }

    /**
     * 标题适配
     */
    class TitleViewHolder extends RecyclerView.ViewHolder{
        private TextView tvTitle;
        public TitleViewHolder(@NonNull View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tv_title);
        }
    }

    /**
     * 内容适配
     */
    class CommonViewHolder extends RecyclerView.ViewHolder{
        private TextView tvNickname;
        private TextView tvAge;
        private TextView tvContactName;
        private TextView tvContactPhone;
        private TextView tvDesc;
        private ImageView ivPhoto;
        private ImageView ivSex;
        private LinearLayout llContactInfo;
        public CommonViewHolder(@NonNull View itemView) {
            super(itemView);
            tvNickname = itemView.findViewById(R.id.tv_nickname);
            tvAge = itemView.findViewById(R.id.tv_age);
            tvContactName = itemView.findViewById(R.id.tv_contact_name);
            tvContactPhone = itemView.findViewById(R.id.tv_contact_phone);
            tvDesc = itemView.findViewById(R.id.tv_desc);
            ivPhoto = itemView.findViewById(R.id.iv_photo);
            ivSex = itemView.findViewById(R.id.iv_sex);
            llContactInfo = itemView.findViewById(R.id.ll_contact_info);
        }
    }

    public interface OnClickListener{
        void OnClick(int position);
    }
}
